import json
from pymud import IConfig, GMCPTrigger

"""
GMCP频道：
北侠命令: tune gmcp、
        set raw_data_format 2  设置 hp * 输出格式
        tune gmcp format raw/pretty 设置中/英文格式
"""


class GMCPChannel(IConfig):
    def __init__(self, session, *args, **kwargs):
        self.session = session
        self.socket = session.application.get_globals("ws")

        self._gmcp_status = [
            GMCPTrigger(
                self.session, "GMCP.raw_hp", group="sys", onSuccess=self.on_all
            ),
            GMCPTrigger(
                self.session,
                "GMCP.Status",
                group="sys",
                onSuccess=self.on_change,
            ),
        ]

    def on_all(self, id, line, wildcards):
        sendData = {"type": "all", "all": wildcards}
        self.session.info(wildcards)
        self.socket.ws.send(json.dumps(sendData))

    def on_change(self, id, line, wildcards):
        sendData = {"type": "change", "change": wildcards}
        self.socket.ws.send(json.dumps(sendData))

    def __unload__(self):
        self.session.delObject(self._gmcp_status)
